<?php
namespace Employee\Controller;
use Common\Controller\AdminbaseController;
// +----------------------------------------------------------------------
// | Date:2016年11月22日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class AdminUserController extends AdminbaseController{
    
    protected $m_position;
    protected $m_department;
    protected $model;
    protected $positionType; 
    protected $userStatus; 
    protected $default_pwd='abc123'; //默认员工密码
    public function _initialize(){
        parent::_initialize();
        $this->m_position = D('EmployeePosition');
        $this->m_position_lvl = D('EmployeePositionLvl');
        $this->m_department = D('EmployeeDepartment');
        $this->model = D('EmployeeUser');
        $this->userStatus = ['1'=>'在职','0'=>'离职'];
        $privilege = [
            '1'=>[
                'name'=>'[1]中心负责人考勤权',
                'rule'=>[
                  '1.短途外出（小于24小时）申请发送至考勤管理员处审批',
                  '2.请假申请（小于24小时）申请发送至考勤管理员处审批',
                  '3.补打卡由考勤管理员审批',
                  '4.以上三点，包括凌晨加班消息，均不发送主管(李总)'
                    ],
            ],
            '2'=>[
                'name'=>'[2]外出申请的审核不经主管，交由行政主管',
                'rule'=>['注意：同时选中[特权1]和[特权2],外出审核人会变成行政主管  '],
            ],
            '3'=>[
                'name'=>'[3]查看高管行程',
                'rule'=>['可以在考勤系统查看高管行程记录'],
            ],
            '4'=>[
                'name'=>'[4]添加高管行程',
                'rule'=>['添加进高管行程管理名单'],
            ]
            
            ];
        
        
        $this->assign('userStatus',$this->userStatus);
        $this->assign('isAttendance',['1'=>'否','0'=>'是']);
        $this->assign('sex',['1'=>'男','2'=>'女']);
        $this->assign('privilege',$privilege);
    }
    
    public function index(){
        $search = I('search');
        if ($search['ep_name']) $where['ep_name'] = ['like',"%{$search['ep_name']}%"];
        if ($search['sn']) $where['sn'] = $search['sn'];
        if ($search['position_lvl_id']) $where['position_lvl_id'] = $search['position_lvl_id'];
        if (isset($search['status']) && $search['status'] !='') $where['status'] = $search['status'];
        if ($search['department_id']) {
            //获取下级部门id
            $child_id = get_child_department_id($search['department_id']);
            
            $where['department_id'] = ['in',$child_id];
        }
        $model = $this->model->alias('user')->join([
            'LEFT JOIN __EMPLOYEE_DEPARTMENT__ dep ON dep.dep_id = user.department_id',
            'LEFT JOIN __EMPLOYEE_POSITION_LVL__ plv ON plv.plv_id = user.position_lvl_id',
        ]);
        
        $field='plv_title,ep_id,ep_name,status,dep_name,user.created_time,email,sn,join_date';
        
        $list = $this->lists($model,$where,$order='user.created_time DESC',$field);
        $this->assign('list',$list);
        
        $this->assign('positionLvl',$this->_postionLvl());
        $this->display();
    }
    
    
    
    public function edit(){
        
        $id = I('id');
        if (IS_POST){
           $post = I('post');
           $post['updated_time'] =CURRENT_TIME;
           $where['ep_id'] = $id;
           $post['position_id'] = $this->m_position->position_id($post['position_name']);
           $post['ep_name'] = trim($post['ep_name']);
           $post['sn'] = trim($post['sn']);
           $post['email'] = trim($post['email']);
           
       //    $this->check_email($post['email']);
           
           if (empty($post['quit_date'])) $post['quit_date'] = null;
           if ($post['status'] == 0 && empty($post['quit_date'])) {
               $this->error('请输入离职时间！！');
           }
           if($post['privilege']) {
               $post['privilege'] = implode(',',$post['privilege']);
           }else{
               $post['privilege'] = '';
           }
           $ret = $this->model->where($where)->save($post);
           if (!$ret) $this->error('操作失败！');
           if (!$this->model->create_approver_list()) $this->error('审核人名单数据生成失败！');
           $this->success('操作成功！');
           exit();
        }
        //是否显示重置密码按钮
        $info = $this->model->where(['ep_id'=>$id])->find();
        $info['position_name'] = $this->m_position->where(['pos_id'=>$info['position_id']])->getField('pos_title');
        if (!empty($info['privilege'])){
            $info['privilege'] = explode(',',$info['privilege']);
        }
        $showReset = $info['password'] !== sp_password($this->default_pwd) ? true : false;
        
        
        $approver = $this->model->where(['ep_id'=>$info['approver_id']])->field('ep_id,ep_name')->find();
        
        $this->assign('leader',$this->getLeader($info['department_id']));
      
        $this->assign('approver',$approver);
        $this->assign('showReset',$showReset);
        $this->assign('positionLvl',$this->_postionLvl());
        $this->assign('info',$info);
        $this->display();
    }
    
    public function check_email($email){
        $ret = $this->model->where(['email'=>$email])->find();
        $errormsg = "该邮箱已存在:{$ret['ep_name']}[{$ret['sn']}]";
        if ($ret) $this->error($errormsg);
        
    }
    
    
    /**
     * 根据部门，获取当前部门职级在1和2的员工
     * 
     * date:2017年1月19日
     * author: EK_熊
     */
    public function getLeader($dep_id){
        //如果是总监级别，领导可能不是
        $join = [
            'LEFT JOIN __EMPLOYEE_USER__ as u ON dep.leader_uid = u.ep_id'
        ];
        $boss = D('EmployeeDepartment')->alias('dep')->join($join)->where(['dep.dep_id'=>$dep_id])->field('ep_id,ep_name')->find();
        if (IS_AJAX) {
            $this->success($boss);
            exit();
        }
        return $boss;
    }
    
    
    
    public function add(){
        if (IS_POST) {
            $post = I('post');
            $isExist = $this->model->where(['ep_name'=>$post['ep_name'],'sn'=>$post['sn']])->find();
            if ($isExist) $this->error('该员工数据已存在，请勿重复添加！');
            
            $post['created_time'] = $post['updated_time'] =CURRENT_TIME;
            $post['password'] = sp_password($this->default_pwd);
            $post['position_id'] = $this->m_position->position_id($post['position_name']);
            $post['ep_name'] = trim($post['ep_name']);
            $post['email'] = trim($post['email']);
            
            $this->check_email($post['email']);
            $this->check_name($post['ep_name']);
            
            $ret = $this->model->add($post);
            if (!$ret) $this->error('添加失败！');
            //更新工号
            $this->model->where(['ep_id'=>$ret])->setField('sn',$ret);
            if (!$this->model->create_approver_list()) $this->error('审核人名单数据生成失败！');
            
            $this->success('添加成功！');
            exit();
        }
        
        $this->assign('position',$this->m_position->getAll());
        $this->assign('positionLvl',$this->_postionLvl());
        $this->display();
    }
    
    /**
     * 检查姓名是否重复
     * 
     * date:2017年10月18日
     * author: EK_熊
     */
    public function check_name($name){
        
        $name = $this->model->where(['ep_name'=>$name])->find();
        if ($name) $this->error('该姓名已存在,如果确实是同名员工，请添加数值方便区分，如[李小红2]');
    }
    
    
    private function _postionLvl(){
        $positionLvl = $this->m_position_lvl->where(['status'=>1])->getField('plv_id,plv_title',true);
        return $positionLvl;
    }
    
    public function restPassword(){
        $uid = I('uid');
        $ret = $this->model->where(['ep_id'=>$uid])->setField('password',sp_password($this->default_pwd));
        if (!$ret) $this->error('数据异常，重置失败，请联系管理员');
        $this->success('重置成功,密码为abc123');
    }
    
    
}